/**
 * All rights Reserved, Designed By 信鸥科技
 * project : lawyer-design
 * Created by youquer on 17/12/5.
 * 注意：本内容仅限于信鸥科技有限公司内部传阅，禁止外泄以及用于其他的商业目的
 * Description:
 *
 */
/**
 * All rights Reserved, Designed By 信鸥科技
 * project : committee
 * Created by youquer on 17/10/25.
 * 注意：本内容仅限于信鸥科技有限公司内部传阅，禁止外泄以及用于其他的商业目的
 * Description:
 *
 */

import { postList, postUpdate, postAdd, postDel, postGet } from '../services/tab';
import { message } from 'antd';
import queryString from 'query-string';


export default {
  namespace: 'tab',
  state: {
    //分页
    page: 1, //当前页
    pageSize: 10, //每页条数
    total: 0, //总页数
    //列表页-添加页提交按钮loading
    loading: false,//添加页确定按钮loading
    dataList: [], //列表页数据数据

    //字段
    id: '',
    name: '', //标签名称
    type: 1, //标签 2-分类
    classify: '0', //1-新闻 2-产品
    //sort: 0,
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
    clear(state){
      return {
        ...state,
        ...{
          //分页
          page: 1, //当前页
          pageSize: 10, //每页条数
          total: 0, //总页数
          //列表页-添加页提交按钮loading
          loading: false,//添加页确定按钮loading
          dataList: [], //列表页数据数据

          //字段
          id: '',
          name: '', //标签名称
          type: 1, //标签 2-分类
          classify: '1', //1-新闻 2-产品
          //sort: 0,
        },
      };
    },
  },


  effects:{
    //列表
    *fetch({ payload }, { call, put, select }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      var date = yield select(state => state.tab);
      if (!payload){
        payload = {};
      }
      payload['page'] = date.page;
      payload['pageSize'] = date.pageSize;
      payload['name'] = date.name;
      payload['classify'] = [];
      if (date.classify instanceof Array){
        payload['classify'] = date.classify;
      }
      console.log(payload);
      const data = yield call(postList, payload);
      console.log(data);
      if (data){
        if (data.recode === 200){
          yield put({
            type: 'save',
            payload: data.body
          });

        } else if (data.recode == 9000){
          message.error("您的权限不足");
        } else if (data.recode == 1001){
          window.location.hash = "/user/login"
        } else {
          message.error('网络请求失败!');
        }
      } else {
        message.error('网络请求失败!');
      }

      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
    //删除
    *del({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const data = yield call(postDel, payload);
      if (data){
        if (data.recode === 200){

          message.success('删除标签成功');
          yield put({
            type: 'fetch',
          })

        } else if (data.recode == 9000){
          message.error("您的权限不足");
        } else if (data.recode == 1001){
          window.location.hash = "/user/login"
        } else {
          message.error('网络请求失败!');
        }
      } else {
        message.error('网络连接失败');
      }
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
    //添加
    *add({ payload }, { call, put, select }) {
      yield put({
        type: 'changeLoading',
        payload: true
      });

      var date = yield select(state => state.tab);
      if (!payload){
        payload = {};
      }
      payload['name'] = date.name;
      payload['type'] = date.type;
      payload['classify'] = date.classify;
      if (payload.name && payload.classify != '0'){
        const data = yield call(postAdd, payload);
        if (data.recode == 200){
          yield put({
            type: "clear"
          });
          window.location.hash = "/data/tab";
        } else if (data.recode == 9000){
          message.error("您的权限不足");
        } else if (data.recode == 1001){
          window.location.hash = "/user/login"
        } else {
          message.error('网络请求失败!');
        }

      } else {
        message.warning('所有信息均为必填信息');
      }

      yield put({
        type: 'changeLoading',
        payload: false
      });
    },
    //获取修改信息
    *get({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const data = yield call(postGet, payload);
      if (data){
        if (data.recode == 200) {
          yield put({
            type: 'save',
            payload: data.body.info
          });
        } else if (data.recode == 9000){
          message.error("您的权限不足");
        } else if (data.recode == 1001){
          window.location.hash = "/user/login"
        } else {
          message.error('网络请求失败!');
        }
      } else {
        message.error('网络请求失败!');
      }
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
    //修改
    *update({ payload }, { call, put, select }) {
      yield put({
        type: 'changeLoading',
        payload: true
      });

      var date = yield select(state => state.tab);
      if (!payload){
        payload = {};
      }
      payload['id'] = date.id;
      payload['name'] = date.name;
      payload['type'] = date.type;
      payload['classify'] = date.classify;
      if (payload.name && payload.classify != '0'){
        const data = yield call(postUpdate, payload);
        if (data.recode == 200){
          yield put({
            type: "clear"
          });
          window.location.hash = "/data/tab";
        } else if (data.recode == 9000){
          message.error("您的权限不足");
        } else if (data.recode == 1001){
          window.location.hash = "/user/login"
        } else {
          message.error('网络请求失败!');
        }

      } else {
        message.warning('所有信息均为必填信息');
      }

      yield put({
        type: 'changeLoading',
        payload: false
      });
    },


  },
}
